﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace GShare1._0.Server
{

    public partial class GUI_Server : Form
    {
        MainThreadInvoker mainInvoker;
        MessageBoxInvoker mboxInvoker;
        public Server server;
        ClientListUpdateDelegate listInvoker;
        public GUI_Server()
        {
            mboxInvoker = new MessageBoxInvoker(MboxInvoker);
            mainInvoker = new MainThreadInvoker(Invoker);
            listInvoker = new ClientListUpdateDelegate(ListUpdater);

            InitializeComponent();
            //label1.Text = GetExternalIP().ToString();
        }

        private void Invoker(Delegate MI)
        {
            //MethodInvoker invoke = new MethodInvoker(delegate
            //    {
            //        MessageBox.Show(msg);
            //    });
            this.BeginInvoke(MI);
        }

        private void ListUpdater(List<Client> clientList)
        {
            MethodInvoker invoker = new MethodInvoker(delegate
                {

                    listView1.Items.Clear();
                    if (clientList.Count != 0)
                    {
                        foreach (Client client in clientList.ToArray())
                        {
                            ListViewItem clientItem = new ListViewItem(client.clientInfo.UserName);
                            clientItem.SubItems.Add(client._clientSocket.RemoteEndPoint.ToString());
                            //clientItem.SubItems.Add(client.AdminUser.ToString());
                            clientItem.SubItems.Add(client.clientInfo.ConnectionTime.ToString());
                            clientItem.Tag = client;
                            listView1.Items.Add(clientItem);
                        }
                    }
                });
            this.BeginInvoke(invoker);
        }



        private void MboxInvoker(string msg)
        {
            MethodInvoker del = new MethodInvoker(delegate
            {
                textBox1.Text += msg + Environment.NewLine;
            });
            //Invoke this on main thread - Here we decide where to invoke could just run here
            this.BeginInvoke(del);
        }

        public static IPAddress GetExternalIP()
        {
            string url = "http://bot.whatismyipaddress.com";
            WebClient webClient = new WebClient();
            string response = Encoding.UTF8.GetString(webClient.DownloadData(url));
            IPAddress ip = IPAddress.Parse(response);
            return ip;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (server == null)
            {
                server = new Server(mboxInvoker, listInvoker);
                server.Start();
            }
            else
            {
                MessageBox.Show("Server Already Running");
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (server != null)
            {
                server.Shutdown();
                server = null;
                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 0)
            {
                //Get the selected client from the list and kick
                Client client = (Client)listView1.SelectedItems[0].Tag;
                client.Disconnect();

            }
        }



    }
}
